import logging

from database import R
import requests


def getToken():
    token = R.get('access_token')
    if token is None:
        logging.info('access-token expired refresh')
        # 获得 access token
        res = requests.get(
            'https://api.weixin.qq.com/cgi-bin/token?grant_type=client_credential&appid=wx7d2fee7c9f9d6b75&secret=b69085669731f3408f443f0caf568787').json()
        print(res['access_token'])
        R.set('access_token', res['access_token'], ex=3600)
        return res['access_token']
    else:
        return token
